package ganga.custodian;

import android.content.Context;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

public class CustodianPowerManager {
	public PowerManager powerManager;
	private WakeLock screenWakeLock;
	private WakeLock cpuWakeLock;

	public CustodianPowerManager(Context context) {
		powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
	}

	/**
	 * Releases all wake locks.
	 */
	public void release() {
		releaseScreenWakeLock();
		releaseCpuWakeLock();
	}

	/**
	 * Ensures that the CPU is running; the screen and keyboard backlight will
	 * be allowed to go off.
	 */
	public void acquireCpuWakeLock() {
		cpuWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Helper.TAG);
		cpuWakeLock.acquire();
	}

	public void acquireScreenWakeLock() {
		screenWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE
				| PowerManager.ACQUIRE_CAUSES_WAKEUP, Helper.TAG);
		screenWakeLock.acquire();
	}

	/**
	 * This method releases your claim to the CPU being on.
	 */
	public void releaseCpuWakeLock() {
		if (cpuWakeLock == null)
			return;
		cpuWakeLock.release();
		cpuWakeLock = null;
	}

	/**
	 * Releases your claim to the screen being on.
	 */
	public void releaseScreenWakeLock() {
		if (screenWakeLock == null)
			return;
		screenWakeLock.release();
		screenWakeLock = null;
	}

}
